import Mock from 'mockjs'
import '@/mock/extend'
import {parseUrlParams} from '@/utils/request'
const Random = Mock.Random;

const source = Mock.mock({
  'list|100': [{
    'id|+1': 1,
    'timestamp': () => Random.datetime('yyyy-MM-dd HH:mm:ss'), // 随机生成监测时间
    'temperature|0-100.2': 0, // 随机生成温度，保留两位小数
    'density|0-100.2': 0, // 随机生成密度，保留两位小数
    'viscosity|0-100.2': 0, // 随机生成粘度，保留两位小数
    'dielectricConstant|0-100.2': 0, // 随机生成介电常数，保留两位小数
    'waterContent|0-100.2': 0, // 随机生成含水量，保留两位小数
    'A_70_90um|0-100.2': 0, // 随机生成A：70-90μm 数据，保留两位小数
    'C_110_140um|0-100.2': 0, // 随机生成C：110-140μm 数据，保留两位小数
    'B_90_110um|0-100.2': 0, // 随机生成B：90-110μm 数据，保留两位小数
  }]
});

Mock.mock(RegExp(`${process.env.VUE_APP_API_BASE_URL}/detectionList` + '.*'),'get', ({url}) => {
  const params = parseUrlParams(decodeURI(url))
  let {page, pageSize} = params
  page = eval(page) - 1 || 0
  pageSize = eval(pageSize) || 10

  delete params.page
  delete params.pageSize

  let result = source.list.filter(item => {
    for (let [key, value] of Object.entries(params)) {
      if (item[key] !== value) {
        return false
      }
    }
    return true
  })
  const total = result.length
  if ((page) * pageSize > total) {
    result = []
  } else {
    result = result.slice(page * pageSize, (page + 1) * pageSize)
  }

  return {
    code: 0,
    message: 'success',
    data: {
      page: page + 1,
      pageSize,
      total: 100,
      list: result
    }
  }
})
